package com.yx.cloud.activity;

import android.app.Dialog;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.iot.sweeper.AppContext;
import com.yx.cloud.R;
import com.yx.cloud.adapter.LanguageAdapter;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.constant.Constants;
import com.yx.cloud.databinding.SwitchlanguageActivityBinding;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.LanguageUtils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.SharedPrefsUtil;

import java.util.Locale;

/**
 * 创建时间 : 2018/12/26.
 * 创建人 :
 * 功能描述 :切换语言界面
 */
public class SwitchLanguageActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "SwitchLanguageActivity";
    String mLanguage;
    private SwitchlanguageActivityBinding binding;
    private String[] language = null;

    @Override
    public void onCreate() {
        binding = SwitchlanguageActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());


        language = new String[]{Constants.ZH, Constants.TW, Constants.EN, Constants.DE, Constants.RU,
                Constants.FR, Constants.PT, Constants.JA, Constants.ES, Constants.IT, Constants.PL,
                Constants.KO, Constants.HI, Constants.IN, Constants.TH, Constants.MS, Constants.VI,
                Constants.IW, Constants.AR};

        LanguageAdapter adapter = new LanguageAdapter(this, language);
        binding.languageView.setAdapter(adapter);
        // 设置列表样式
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        binding.languageView.setLayoutManager(layoutManager);

        adapter.setOnItemClickListener(new LanguageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                // LogUtils.i("LanguageAdapter onItemClick ----");
                if (!mLanguage.equals(language[position])){
                    switchLanguageDialog(getString(R.string.MY_LanguageSwitch_Tip), language[position], position);
                }
            }
        });
        mLanguage = SharedPrefsUtil.getValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", "");
        //LogUtils.i("SwitchLanguageActivity mLanguage ----" + mLanguage);
        if (mLanguage.equals("")) {
            //如果不进行手动设置，每次就默认系统的
            mLanguage = LanguageUtils.getSystemLang(this);
        }
        //LogUtils.i("SwitchLanguageActivity mLanguage 2222----" + mLanguage);
        for (int i = 0; i < language.length; i++) {
            if (mLanguage.equals(language[i])) {
                // LogUtils.i(mLanguage + " : SwitchLanguageActivity tttt ----" + language[i]);
                adapter.setCurrentPosition(i, true);
                adapter.notifyDataSetChanged();
            }
        }
        binding.ivSwitchLanguageReturn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_switchLanguageReturn:
                Intent_utils.backIntent(this);
                break;

        }
    }

    private void switchLanguageDialog(String dialogTitle, String tag, int position) {
        Dialog dialog = new Dialog(SwitchLanguageActivity.this);
        dialog.setContentView(R.layout.content_centered_dialog);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.setCanceledOnTouchOutside(false);
        TextView mBtnOutLoginConfirm = dialog.findViewById(R.id.btn_centered_Confirm);
        TextView mBtnOutLoginCancel = dialog.findViewById(R.id.btn_centered_Cancel);
        TextView mTvDialogTitle = dialog.findViewById(R.id.tv_centered_dialogTitle);

        mTvDialogTitle.setText(dialogTitle);
        mBtnOutLoginConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                if (tag.equals(Constants.ZH)) {
                    //中文
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale.SIMPLIFIED_CHINESE);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.ZH);
                } else if (tag.equals(Constants.TW)) {
                    //台湾繁体
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale.TAIWAN);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.TW);
                } else if (tag.equals(Constants.EN)) {
                    //英文
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale.US);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.EN);
                } else if (tag.equals(Constants.DE)) {
                    //德语
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale.GERMAN);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.DE);
                } else if (tag.equals(Constants.RU)) {
                    //俄语
                    Locale Locale_Russia = new Locale("RU", "ru", "");
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale_Russia);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.RU);
                } else if (tag.equals(Constants.FR)) {
                    //法语
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale.FRENCH);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.FR);
                } else if (tag.equals(Constants.PT)) {
                    //葡萄牙
                    Locale Locale_Portugal = new Locale("PT", "pt", "");
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale_Portugal);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.PT);
                } else if (tag.equals(Constants.JA)) {
                    //日语
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale.JAPANESE);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.JA);
                } else if (tag.equals(Constants.ES)) {
                    //西班牙
                    Locale Locale_Spain = new Locale("ES", "es", "");
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale_Spain);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.ES);
                } else if (tag.equals(Constants.IT)) {
                    //意大利
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale.ITALIAN);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.IT);
                } else if (tag.equals(Constants.PL)) {
                    //波兰
                    Locale Locale_Polish = new Locale("PL", "pl", "");
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale_Polish);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.PL);
                } else if (tag.equals(Constants.KO)) {
                    //韩语
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale.KOREAN);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.KO);
                } else if (tag.equals(Constants.HI)) {
                    //印地语
                    Locale Locale_Hindi = new Locale("HI", "hi", "");
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale_Hindi);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.HI);
                } else if (tag.equals(Constants.IN)) {
                    //印尼语
                    Locale Locale_Indonesian = new Locale("IN", "in", "");
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale_Indonesian);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.IN);
                } else if (tag.equals(Constants.TH)) {
                    //泰语
                    Locale Locale_Thai = new Locale("TH", "th", "");
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale_Thai);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.TH);
                } else if (tag.equals(Constants.MS)) {
                    //马来语
                    Locale Locale_Malay = new Locale("MS", "ms", "");
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale_Malay);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.MS);
                } else if (tag.equals(Constants.VI)) {
                    //越南语
                    Locale Locale_Vietnamese = new Locale("VI", "vi", "");
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale_Vietnamese);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.VI);
                } else if (tag.equals(Constants.IW)) {
                    //希伯来语
                    Locale Locale_Vietnamese = new Locale("IW", "iw", "");
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale_Vietnamese);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.IW);
                } else if (tag.equals(Constants.AR)) {
                    //阿拉伯语
                    Locale Locale_Vietnamese = new Locale("AR", "EG", "");
                    LanguageUtils.changeAppLanguage(SwitchLanguageActivity.this, Locale_Vietnamese);
                    SharedPrefsUtil.putValue(SwitchLanguageActivity.this, Constants.LANGUAGE, "language", Constants.AR);
                }
                AppContext.get().setRecreate(1);// 退出界面 通知主界面切换语言

                dialog.dismiss();
                finish();
                Intent_utils.enterIntent(SwitchLanguageActivity.this, MainBottomTabActivity.class);
            }
        });
        mBtnOutLoginCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();

            }
        });
        dialog.show();
    }
}
